package com.scriptback.controller;

import com.scriptback.dto.common.ApiResponse;
import com.scriptback.dto.common.PageResponse;
import com.scriptback.dto.script.ScriptCreateRequestDTO;
import com.scriptback.dto.script.ScriptResponseDTO;
import com.scriptback.dto.script.ScriptUpdateRequestDTO;
import com.scriptback.entity.Scripts;
import com.scriptback.service.ProjectService;
import com.scriptback.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 脚本管理控制器
 */
@Controller
@RequestMapping("/scripts")
public class ScriptController {

    @Autowired
    private ScriptService scriptService;

    @PostMapping
    @ResponseBody
    public ApiResponse<ScriptResponseDTO> createScript(@RequestBody ScriptCreateRequestDTO scriptCreateRequestDTO) {

        try{

            Scripts script = scriptService.createScript(scriptCreateRequestDTO.getProject_id(),
                    scriptCreateRequestDTO.getScript_name(),
                    scriptCreateRequestDTO.getScript_description(),
                    scriptCreateRequestDTO.getScript_code());

            ScriptResponseDTO scriptResponseDTO = ScriptResponseDTO.fromEntity(script);

            return ApiResponse.success("创建脚本成功",scriptResponseDTO);

        }catch(Exception e){

            return ApiResponse.error("创建脚本失败: "+e.getMessage());

        }


    }

    @GetMapping
    @ResponseBody
    public ApiResponse<PageResponse<ScriptResponseDTO>> getScripts(
            @RequestParam (required = false)String project_id,
            @RequestParam (defaultValue = "0") int page,
            @RequestParam (defaultValue = "10")int size){

        try{

            Pageable pageable = PageRequest.of(page, size);
            Page<Scripts> allScripts ;

            if(project_id != null && !project_id.trim().isEmpty()){

                allScripts = scriptService.getScriptsByProjectId(project_id,pageable);

            }else{
                allScripts = scriptService.getAllScripts(pageable);
            }

            List<ScriptResponseDTO> content = allScripts.getContent().stream()
                    .map(ScriptResponseDTO::fromEntity)
                    .collect(Collectors.toList());

            PageResponse<ScriptResponseDTO> scriptResponseDTOPageResponse = new PageResponse<>(content,
                    allScripts.getNumber(),
                    allScripts.getSize(),
                    allScripts.getTotalElements(),
                    allScripts.getTotalPages(),
                    allScripts.isFirst(),
                    allScripts.isLast());

            return ApiResponse.success("获取脚本数据成功",scriptResponseDTOPageResponse);

        }catch (Exception e){

            return ApiResponse.error("获取脚本数据失败: "+e.getMessage());

        }




    }


    @GetMapping("/{id}")
    @ResponseBody
    public ApiResponse<ScriptResponseDTO> getScript(@PathVariable String id) {

        try{

            ScriptResponseDTO scriptResponseDTO = ScriptResponseDTO.fromEntity(scriptService.getScriptById(id));

            return ApiResponse.success("成功通过id获取项目",scriptResponseDTO);
        }catch(Exception e){
            return ApiResponse.error("通过id获取项目失败: "+e.getMessage());
        }


    }

    @PostMapping("/{id}/update")
    @ResponseBody
    public ApiResponse<ScriptResponseDTO> updateScript(
            @RequestBody ScriptUpdateRequestDTO request){

        try{

            ScriptResponseDTO scriptResponseDTO = ScriptResponseDTO.fromEntity(scriptService.updateScript(request.getScript_id(), request));

            return ApiResponse.success("更新成功", scriptResponseDTO);
        } catch (Exception e) {

            return ApiResponse.error("更新脚本失败: " + e.getMessage());
        }


    }

    @PostMapping("/{id}/delete")
    @ResponseBody
    public ApiResponse<Void> deleteScript(@PathVariable String id) {
        try {
            scriptService.deleteScript(id);
            return ApiResponse.success("删除成功", null);
        } catch (Exception e) {
            return ApiResponse.error("删除脚本失败: " + e.getMessage());
        }
    }


}
